* 0 => NS_MAIN
[lhc/web/wiklou.git] / includes / Namespace.php
1 <?php
2 /**
3 * Provide things related to namespaces
4 * @package MediaWiki
5 */
6
7 /**
8 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
9 */
10 if( defined( 'MEDIAWIKI' ) ) {
11
12
13 /**
14 * Definitions of the NS_ constants are in Defines.php
15 * @private
16 */
17 $wgCanonicalNamespaceNames = array(
18 NS_MEDIA => 'Media',
19 NS_SPECIAL => 'Special',
20 NS_TALK => 'Talk',
21 NS_USER => 'User',
22 NS_USER_TALK => 'User_talk',
23 NS_PROJECT => 'Project',
24 NS_PROJECT_TALK => 'Project_talk',
25 NS_IMAGE => 'Image',
26 NS_IMAGE_TALK => 'Image_talk',
27 NS_MEDIAWIKI => 'MediaWiki',
28 NS_MEDIAWIKI_TALK => 'MediaWiki_talk',
29 NS_TEMPLATE => 'Template',
30 NS_TEMPLATE_TALK => 'Template_talk',
31 NS_HELP => 'Help',
32 NS_HELP_TALK => 'Help_talk',
33 NS_CATEGORY => 'Category',
34 NS_CATEGORY_TALK => 'Category_talk',
35 );
36
37 if( is_array( $wgExtraNamespaces ) ) {
38 $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces;
39 }
40
41 /**
42 * This is a utility class with only static functions
43 * for dealing with namespaces that encodes all the
44 * "magic" behaviors of them based on index. The textual
45 * names of the namespaces are handled by Language.php.
46 *
47 * These are synonyms for the names given in the language file
48 * Users and translators should not change them
49 *
50 * @package MediaWiki
51 */
52 class Namespace {
53
54 /**
55 * Check if the given namespace might be moved
56 * @return bool
57 */
58 function isMovable( $index ) {
59 return !( $index < NS_MAIN || $index == NS_IMAGE || $index == NS_CATEGORY );
60 }
61
62 /**
63 * Check if the given namespace is not a talk page
64 * @return bool
65 */
66 function isMain( $index ) {
67 return ! Namespace::isTalk( $index );
68 }
69
70 /**
71 * Check if the give namespace is a talk page
72 * @return bool
73 */
74 function isTalk( $index ) {
75 return ($index > NS_MAIN) // Special namespaces are negative
76 && ($index % 2); // Talk namespaces are odd-numbered
77 }
78
79 /**
80 * Get the talk namespace corresponding to the given index
81 */
82 function getTalk( $index ) {
83 if ( Namespace::isTalk( $index ) ) {
84 return $index;
85 } else {
86 # FIXME
87 return $index + 1;
88 }
89 }
90
91 function getSubject( $index ) {
92 if ( Namespace::isTalk( $index ) ) {
93 return $index - 1;
94 } else {
95 return $index;
96 }
97 }
98
99 /**
100 * Returns the canonical (English Wikipedia) name for a given index
101 */
102 function getCanonicalName( $index ) {
103 global $wgCanonicalNamespaceNames;
104 return $wgCanonicalNamespaceNames[$index];
105 }
106
107 /**
108 * Returns the index for a given canonical name, or NULL
109 * The input *must* be converted to lower case first
110 */
111 function getCanonicalIndex( $name ) {
112 global $wgCanonicalNamespaceNames;
113 static $xNamespaces = false;
114 if ( $xNamespaces === false ) {
115 $xNamespaces = array();
116 foreach ( $wgCanonicalNamespaceNames as $i => $text ) {
117 $xNamespaces[strtolower($text)] = $i;
118 }
119 }
120 if ( array_key_exists( $name, $xNamespaces ) ) {
121 return $xNamespaces[$name];
122 } else {
123 return NULL;
124 }
125 }
126 }
127
128 }
129 ?>